# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : __init__.py
# Time       ：2023/2/19 21:05
# Author     ：Y-aong
# version    ：python 3.7
# Description：
"""
import os.path

from celery import Celery
from flask import Flask
from flask_cors import CORS

from public.api_utils.orderlines_plugin import OrderlinesPlugHelper

from public.api_utils.permission_handlers import PermissionAuth

celery = Celery(__name__)
celery.config_from_object('tasks.celery_config')


def _register_plugin(app):
    from public.api_utils.default_config_plugin import DefaultConfig
    default_config = DefaultConfig()
    default_config.init_app(app)
    OrderlinesPlugHelper().init_plugin()


def _register_webhook(app):
    permission_auth = PermissionAuth()
    permission_auth.init_app(app)


def _register_db(app: Flask):
    from public.base_model import db
    from apis.orderlines.models import (
        ProcessInstance, Process, Task, TaskInstance, Variable, VariableInstance, ScheduleTask)
    from apis.schedule_plan.models import IntervalPlan, DatePlan, CrontabPlan, ApschedulerJobs
    from apis.system_oauth.models import (
        SystemUser, SystemRole, SystemPermission, SystemGroup, SystemDepartment, SystemUserRoleRelation,
        SystemUserGroupRelation, SystemGroupPermissionRelation, SystemRolePermissionRelation)
    from apis.config.models import BaseConfig
    from apis.notice.model.alarm import Alarm
    from apis.notice.model.logger_info import LoggerInfo
    from apis.plugin.models.plugin_info import PluginInfo

    db.init_app(app)
    with app.app_context():
        db.create_all()


def _register_resource(app):
    from apis.orderlines import orderlines_ns
    from apis.schedule_plan import schedule_plan_ns
    from apis.system_oauth import system_oauth_ns
    from apis.config import conf_ns
    from apis.token import token_ns
    from apis.flow import flow_ns
    from apis.options import option_ns
    from apis.plugin import plugin_ns
    from apis.home import home_ns
    from apis.notice import notice_ns
    from apis.data_screen import data_screen_ns
    from public.base_view import base_api

    base_api.add_namespace(orderlines_ns)
    base_api.add_namespace(orderlines_ns)
    base_api.add_namespace(system_oauth_ns)
    base_api.add_namespace(conf_ns)
    base_api.add_namespace(token_ns)
    base_api.add_namespace(flow_ns)
    base_api.add_namespace(option_ns)
    base_api.add_namespace(plugin_ns)
    base_api.add_namespace(home_ns)
    base_api.add_namespace(notice_ns)
    base_api.add_namespace(data_screen_ns)
    base_api.init_app(app)


def create_app():
    src_file_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    template = os.path.join(src_file_path, 'templates')
    app = Flask(__name__, template_folder=template)
    app.config.from_object('conf.config.FlaskConfig')
    CORS(app, origins="*", supports_credentials=True)
    _register_db(app)
    _register_resource(app)
    _register_webhook(app)
    _register_plugin(app)

    return app
